package com.xcalendar.service.rest.impl;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.google.appengine.api.datastore.KeyFactory;
import com.xcalendar.database.dao.UserDAO;
import com.xcalendar.database.table.Event;
import com.xcalendar.database.table.User;
import com.xcalendar.management.EventManager;
import com.xcalendar.managementImpl.EventManagerImpl;
import com.xcalendar.service.rest.converter.UserConverter;


@Path("/EditEvent/")
public class EditEventResource {

	
	@GET
	@Produces({ "application/json" })
	@Path("/userName={userName}&eventKey={eventKey}&eventTitle={eventTitle}&eventDescription={eventDescription}&eventStart={eventStart}&eventEnd={eventEnd}&remindTime={remindTime}/")
	public UserConverter addEvent(@PathParam("userName") String userName,@PathParam("eventKey") String eventKey, @PathParam("eventTitle") String eventTitle,  @PathParam("eventDescription") String eventDescription, 
			 @PathParam("eventStart") String eventStart,  @PathParam("eventEnd") String eventEnd, @PathParam("remindTime") String remindTime) {
		UserConverter converter;
		User userTemp = new User();
		userTemp.setUserName(userName);
		UserDAO userDAO = new UserDAO();
		User user = userDAO.readUser(userTemp);
		if(user == null) {
			converter = new UserConverter();
			converter.setError("user error");
		}
		EventManager eventManager = new EventManagerImpl();
		Date dStart = new Date(Long.parseLong(eventStart));
		Date dEnd = new Date(Long.parseLong(eventEnd));
		Date r = new Date(Long.parseLong(remindTime));
		
	
		Event event = new Event();
		event.setKey(KeyFactory.stringToKey(eventKey));
		event.setTitle(eventTitle);
		event.setDescription(eventDescription);
		event.setStartTime(dStart);
		event.setEndTime(dEnd);
		event.setRemindTime(r);
		eventManager.updateEvent(event);
		User userNew = userDAO.readUser(userTemp);
		converter = new UserConverter(userNew);
		
		return converter;
	}
	
}
